package com.wolves.career.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wolves.career.entity.*;
import com.wolves.career.entity.vo.RecruitmentVo;
import com.wolves.career.mapper.JobOpportunityRecruitmentMapper;
import com.wolves.career.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-23
 * 【这里很多代码都是重复的，优点冗余，后期需要优化】
 */
@Service
public class JobOpportunityRecruitmentServiceImpl extends ServiceImpl<JobOpportunityRecruitmentMapper
        , JobOpportunityRecruitment> implements JobOpportunityRecruitmentService {

    @Autowired
    JobOpportunityCompanyService companyService;
    @Autowired
    JobOpportunityPositionService positionService;
    @Autowired
    JobOpportunityRequireService requireService;
    @Autowired
    JobOpportunityTechnologyService technologyService;

    /**
     * 获取所有招聘封装信息
     * @return
     */
    @Override
    public List<RecruitmentVo> getAllRecruitment() {
        //创建最终返回的列表
        List<RecruitmentVo> finalList = new ArrayList<>();
        //查询所有职位
        List<JobOpportunityPosition> positionList = positionService.list(null);
        for (JobOpportunityPosition position : positionList) {
            //获取职位id
            String positionId = position.getPositionId();
            //根据职位id找到所有招聘信息
            List<JobOpportunityRecruitment> recruitmentList = getRecruitmentByPositionId(positionId);
            for (JobOpportunityRecruitment recruitment : recruitmentList) {
                RecruitmentVo recruitmentVo = new RecruitmentVo();
                //获取相应公司
                String companyId = recruitment.getCompanyId();
                JobOpportunityCompany company = companyService.getById(companyId);
                //封装公司信息
                BeanUtils.copyProperties(company, recruitmentVo);
                recruitmentVo.setCompanyName(company.getName());
                //封装招聘信息
                BeanUtils.copyProperties(recruitment, recruitmentVo);
                List<String> technologyList = getTechnologiesByPositionId(positionId);
                //封装技术信息
                recruitmentVo.setTechnologyName(technologyList);


                //获取技术map(id,name)
                HashMap<String, String> technologiesMap = getTechnologiesMapByPositionId2(positionId);
                recruitmentVo.setTechnologyMap(technologiesMap);


                //封装招职位信息
                BeanUtils.copyProperties(position, recruitmentVo);
                recruitmentVo.setPositionName(position.getName());
                finalList.add(recruitmentVo);
            }
        }
        return finalList;
    }

    /**
     * 返回查询的信息（将根据公司名和职位名的查询合并在一起）
     * @param des 查询的信息
     * @return
     */
    @Override
    public List<RecruitmentVo> getRecruitmentByCompanyOrPosition(String des) {
        List<RecruitmentVo> list1 = getRecruitmentVoByCompany(des);
        List<RecruitmentVo> list2 = getRecruitmentVoByPosition(des);
        //将两个列表合并
        list1.addAll(list2);
        return list1;
    }

    /**
     * 根据公司名称获取招聘信息
     *
     * @param des 公司名称
     * @return
     */
    @Override
    public List<RecruitmentVo> getRecruitmentVoByCompany(String des) {
        List<RecruitmentVo> finalList = new ArrayList<>();
        //先模糊查询公司
        QueryWrapper<JobOpportunityCompany> companyWrapper = new QueryWrapper<>();
        companyWrapper.like("name", des);
        //获取了查询的所有公司
        List<JobOpportunityCompany> companyList = companyService.list(companyWrapper);

        for (JobOpportunityCompany company : companyList) {
            //获取公司id
            String companyId = company.getCompanyId();
            //获取所有招聘信息
            List<JobOpportunityRecruitment> recruitmentList = getRecruitmentByCompanyId(companyId);
            //便利所有招聘信息找到职位
            for (JobOpportunityRecruitment recruitment : recruitmentList) {
                String positionId = recruitment.getPositionId();
                //根据职位id获取职位
                JobOpportunityPosition position = getPositionByPositionId(positionId);
                //创建
                RecruitmentVo recruitmentVo = new RecruitmentVo();
                //传递公司的信息
                BeanUtils.copyProperties(company, recruitmentVo);
                recruitmentVo.setCompanyName(company.getName());
                //传递招聘信息
                BeanUtils.copyProperties(recruitment, recruitmentVo);
                //传递职位信息
                BeanUtils.copyProperties(position, recruitmentVo);
                recruitmentVo.setPositionName(position.getName());
                //获取技术要求
                List<String> technologyList = getTechnologiesByPositionId(positionId);
                recruitmentVo.setTechnologyName(technologyList);


                //获取技术map(id,name)
                HashMap<String, String> technologiesMap = getTechnologiesMapByPositionId2(positionId);
                recruitmentVo.setTechnologyMap(technologiesMap);

                finalList.add(recruitmentVo);
            }
        }
        return finalList;
    }

    /**
     * 根据职位名称模糊查询招聘信息
     *
     * @param des 职位名称 用作模糊查询
     * @return
     */
    @Override
    public List<RecruitmentVo> getRecruitmentVoByPosition(String des) {
        //创建最终返回的列表
        List<RecruitmentVo> finalList = new ArrayList<>();
        //根据名称模糊查询所有职位
        QueryWrapper<JobOpportunityPosition> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", des);
        List<JobOpportunityPosition> positionList = positionService.list(queryWrapper);
        for (JobOpportunityPosition position : positionList) {
            //获取职位id
            String positionId = position.getPositionId();
            //根据职位id找到所有招聘信息
            List<JobOpportunityRecruitment> recruitmentList = getRecruitmentByPositionId(positionId);
            for (JobOpportunityRecruitment recruitment : recruitmentList) {
                RecruitmentVo recruitmentVo = new RecruitmentVo();
                //获取相应公司
                String companyId = recruitment.getCompanyId();
                JobOpportunityCompany company = companyService.getById(companyId);
                //封装公司信息
                BeanUtils.copyProperties(company, recruitmentVo);
                recruitmentVo.setCompanyName(company.getName());
                //封装招聘信息
                BeanUtils.copyProperties(recruitment, recruitmentVo);
                List<String> technologyList = getTechnologiesByPositionId(positionId);

                //获取技术map(id,name)
                HashMap<String, String> technologiesMap = getTechnologiesMapByPositionId2(positionId);
                recruitmentVo.setTechnologyMap(technologiesMap);

                //封装技术信息
                recruitmentVo.setTechnologyName(technologyList);
                //封装招职位信息
                BeanUtils.copyProperties(position, recruitmentVo);
                recruitmentVo.setPositionName(position.getName());
                finalList.add(recruitmentVo);
            }
        }
        return finalList;
    }

    /**
     * 根据职位id获取所有招聘信息
     *
     * @param positionId 职位id
     * @return
     */
    public List<JobOpportunityRecruitment> getRecruitmentByPositionId(String positionId) {
        QueryWrapper<JobOpportunityRecruitment> recruitmentQueryWrapper = new QueryWrapper<>();
        recruitmentQueryWrapper.eq("position_id", positionId);
        List<JobOpportunityRecruitment> list = baseMapper.selectList(recruitmentQueryWrapper);
        return list;
    }


    /**
     * 根据公司id获取招聘
     *
     * @param companyId 公司id
     * @return
     */
    @Override
    public List<JobOpportunityRecruitment> getRecruitmentByCompanyId(String companyId) {
        QueryWrapper<JobOpportunityRecruitment> recruitmentQueryWrapper = new QueryWrapper<>();
        recruitmentQueryWrapper.eq("company_id", companyId);
        List<JobOpportunityRecruitment> list = baseMapper.selectList(recruitmentQueryWrapper);
        return list;
    }


    /**
     * 根据positionId获取职位
     *
     * @param positionId
     * @return
     */
    public JobOpportunityPosition getPositionByPositionId(String positionId) {
        JobOpportunityPosition position = positionService.getById(positionId);
        return position;
    }

    /**
     * 根据职位id获取职位需求
     *
     * @param positionId
     * @return
     */
    public List<String> getTechnologiesByPositionId(String positionId) {
        List<String> technologyList = new ArrayList<>();
        //根据公司职位id获取需求
        QueryWrapper<JobOpportunityRequire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("position_id", positionId);
        List<JobOpportunityRequire> requireList = requireService.list(queryWrapper);
        HashMap<String, String> technologyMap = new HashMap<>();
        for (JobOpportunityRequire require : requireList) {
            String technologyId = require.getTechnologyId();
            JobOpportunityTechnology technology = technologyService.getById(technologyId);
            if (technology != null) {
                technologyMap.put(positionId,technology.getName());
                technologyList.add(technology.getName());
            }
        }
        return technologyList;
    }


    /**
     * 根据职位id获取职位需求map
     *
     * @param positionId
     * @return
     */
    public HashMap<String,String> getTechnologiesMapByPositionId2(String positionId) {
        List<String> technologyList = new ArrayList<>();
        //根据公司职位id获取需求
        QueryWrapper<JobOpportunityRequire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("position_id", positionId);
        List<JobOpportunityRequire> requireList = requireService.list(queryWrapper);
        HashMap<String, String> technologyMap = new HashMap<>();
        for (JobOpportunityRequire require : requireList) {
            String technologyId = require.getTechnologyId();
            JobOpportunityTechnology technology = technologyService.getById(technologyId);
            if (technology != null) {
                technologyMap.put(technologyId,technology.getName());
                technologyList.add(technology.getName());
            }
        }
        return technologyMap;
    }
}
